#include "Speech.h"

#include <QDebug>
#include <QVector>
#include <QLocale>


L_Speech::L_Speech(QObject *parent)
    :QObject(parent)
{
    m_Speech = nullptr;
    bVoiceLoadSuccess = false;
}

L_Speech::~L_Speech()
{
}


int L_Speech::engineSelect(QString engineName)
{
    qDebug() << "QTextToSpeech::availableEngines:" << QTextToSpeech::availableEngines();

    if(m_Speech != nullptr)
        delete m_Speech;

    if (engineName == "default")    //sapi
        m_Speech = new QTextToSpeech(this);
    else
        m_Speech = new QTextToSpeech(engineName, this);

    return 0;
}

int L_Speech::locateSelect(int index)
{
    QVector<QLocale> locales = m_Speech->availableLocales();
    qDebug() << "m_Speech->availableLocales():" << locales.length() << locales;

    /*
    QLocale current = m_Speech->locale();
    foreach (const QLocale &locale, locales) {
        QString name(QString("%1 (%2)")
                     .arg(QLocale::languageToString(locale.language()))
                     .arg(QLocale::countryToString(locale.country())));
        QVariant localeVariant(locale);
        ui.language->addItem(name, localeVariant);
        if (locale.name() == current.name())
            current = locale;
    }*/

    if(index >= locales.length())
        return -1;

    QLocale locale = locales.at(index);
    m_Speech->setLocale(locale);

    QString name(QString("%1 (%2)")
                 .arg(QLocale::languageToString(locale.language()))
                 .arg(QLocale::countryToString(locale.country())));
    qDebug() << "Select Locale:" << name;

    bVoiceLoadSuccess = true;
    return 0;
}

int L_Speech::voiceSelect(int index)
{
    if(!bVoiceLoadSuccess)
        return -1;

    QVector<QVoice> m_voices = m_Speech->availableVoices();

    qDebug() << "m_Speech->availableVoices():" << m_voices.length();    // << m_voices;
    QVector<QVoice>::const_iterator ii;
    for(ii = m_voices.constBegin(); ii != m_voices.constEnd(); ii++)
    {
        qDebug() << "Voice:" << ii->name();
    }


    /*
    QVoice currentVoice = m_Speech->voice();
    foreach (const QVoice &voice, m_voices) {
        ui.voice->addItem(QString("%1 - %2 - %3").arg(voice.name())
                          .arg(QVoice::genderName(voice.gender()))
                          .arg(QVoice::ageName(voice.age())));
        if (voice.name() == currentVoice.name())
            ui.voice->setCurrentIndex(ui.voice->count() - 1);
    }
    connect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
    */

    if(index >= m_voices.length())
        return -1;

    QVoice voice = m_voices.at(index);
    m_Speech->setVoice(voice);

    QString name(QString("%1 - %2 - %3").arg(voice.name())
                 .arg(QVoice::genderName(voice.gender()))
                 .arg(QVoice::ageName(voice.age())));
    qDebug() << "Select Voice：" << name;

    return 0;
}

int L_Speech::read(QString text)
{
    //if(!bVoiceLoadSuccess)
    //    return -1;

    if(m_Speech->state() == QTextToSpeech::Ready)
    {
        qDebug() << "read";
        m_Speech->say(text);
        return 0;
    }
    else
    {
        qWarning() << "read Error：" << m_Speech->state();
        return -1;
    }
}
